package com.hzf.wxplatform.web.controller.manager.dneedsDemand;

import com.alibaba.fastjson.JSONArray;
import com.hzf.api.rpc.api.ApiService;
import com.hzf.common.base.BaseController;
import com.hzf.common.util.ByteUtils;
import com.hzf.common.util.RedisUtil;
import com.hzf.config.dao.model.*;
import com.hzf.designer.dao.model.*;
import com.hzf.ucenter.dao.model.UcenterUser;
import com.hzf.ucenter.dao.model.UcenterUserCity;
import com.hzf.ucenter.dao.model.UcenterUserExample;
import com.hzf.wxplatform.common.WxplatformResult;
import com.hzf.wxplatform.common.WxplatformResultConstant;
import com.hzf.wxplatform.common.utils.UcenterUserUtils;
import org.apache.xpath.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/demand")
public class DneedsDemandIndexController  extends BaseController{
    @Autowired
    ApiService apiService;
    @RequestMapping(value="/info")
    private String info (Model model, int id, HttpServletRequest servletRequest){
        HttpSession session = servletRequest.getSession();
        UcenterUser ucenterUser = new UcenterUser();
        byte[]user =  UcenterUserUtils.getUser(session);
        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }

        DneedsDemandExample example  = new DneedsDemandExample();
        example.createCriteria().andIdEqualTo(id);
        DneedsDemand dneedsDemand = apiService.getDneedsDemand(example);
        DneedsBidExample dneedsBidExample = new DneedsBidExample();
        dneedsBidExample.createCriteria().andUserIdEqualTo(ucenterUser.getId()).andDemandIdEqualTo(dneedsDemand.getId());
        DneedsBid dneedsBid = apiService.getDneedsBid(dneedsBidExample);
        if(null!=dneedsBid){
            if(dneedsBid.getState()!=1){

                model.addAttribute("type",1);

            }else if(dneedsBid.getState()==1&&dneedsDemand.getDemandType()==2){

                model.addAttribute("type",2);
            }else if(dneedsBid.getState()==1&&dneedsDemand.getDemandType()==3){
                DneedsDemandSjtExample dneedsDemandSjtExample = new DneedsDemandSjtExample();
                dneedsDemandSjtExample.createCriteria().andDemandIdEqualTo(dneedsDemand.getId());
                DneedsDemandSjt dneedsDemandSjt = apiService.getDneedsDemandSjt(dneedsDemandSjtExample);

                if(null!=dneedsDemandSjt){
                    model.addAttribute("sjttype",1);
                }else{
                    model.addAttribute("sjttype",0);
                }
                DneedsDemandWltExample dneedsDemandWltExample = new DneedsDemandWltExample();
                dneedsDemandWltExample.createCriteria().andDemandIdEqualTo(dneedsDemand.getId());
                DneedsDemandWlt dneedsDemandWlt = apiService.getDneedsDemandWlt(dneedsDemandWltExample);
                if(null!=dneedsDemandWlt){
                    model.addAttribute("wlttype",1);
                }else{
                    model.addAttribute("wlttype",0);

                }
                model.addAttribute("type",2);
            }else if(dneedsBid.getState()==1&&dneedsDemand.getDemandType()==4){
                model.addAttribute("type",4);
            }else if(dneedsBid.getState()==1&&dneedsDemand.getDemandType()==5){
                model.addAttribute("type",5);
            }else if(dneedsBid.getState()==1&&dneedsDemand.getDemandType()==6){
                model.addAttribute("type",5);
            }
            else if(dneedsBid.getState()==1&&dneedsDemand.getDemandType()==7){
                model.addAttribute("type",5);
            }
            else{
                model.addAttribute("type",0);

            }
        } else{
            model.addAttribute("wlttype",0);

            model.addAttribute("type",0);

        }

        ConfigRegionExample configRegionExample = new ConfigRegionExample();
        configRegionExample.createCriteria().andRegionIdEqualTo(dneedsDemand.getCity());
        ConfigRegion configRegion = apiService.selectAddressRegion(configRegionExample);
        ConfigStyleExample configStyleExample = new ConfigStyleExample();
        configStyleExample.createCriteria().andIdEqualTo(dneedsDemand.getStyleId());
        ConfigStyle configStyle = apiService.selectConfigStyleRegion(configStyleExample);
        ConfigPtanliExample configPtanliExample= new ConfigPtanliExample();
        configPtanliExample.createCriteria().andIdEqualTo(dneedsDemand.getStyleAnli());
        ConfigPtanli configPtanli = apiService.selectConfigPtanliRegion(configPtanliExample);
        UcenterUserExample ucenterUserExample = new UcenterUserExample();
        ucenterUserExample.createCriteria().andIdEqualTo(dneedsDemand.getUserId());
        UcenterUser users= apiService.getUser(ucenterUserExample);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String str=sdf.format(dneedsDemand.getCreateDate());
        model.addAttribute("date",str);
        model.addAttribute("users",users);
        model.addAttribute("data",dneedsDemand);
        model.addAttribute("configRegion",configRegion);
        model.addAttribute("configStyle",configStyle);
        model.addAttribute("configPtanli",configPtanli);

        return thymeleaf("/SJS/SJS_jidanXQ");
    }
    //投标
    @RequestMapping(value="/toubiao",method = RequestMethod.POST)
    @ResponseBody
    private Object toubiao(DneedsBid dneedsBid, Model model,HttpServletRequest request){
        HttpSession session = request.getSession();
        UcenterUser ucenterUser = new UcenterUser();
        byte[]user =  UcenterUserUtils.getUser(session);
        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        DneedsDemandExample dneedsDemandExample = new DneedsDemandExample();
        dneedsDemandExample.createCriteria().andIdEqualTo(dneedsBid.getDemandId());
        DneedsDemand dneedsDemand = apiService.getDneedsDemand(dneedsDemandExample);
        dneedsDemand.setDemandType(1);
        apiService.updateDneedsDemand(dneedsDemand);
        dneedsBid.setCreateDate(new Date());
        dneedsBid.setState(0);
        dneedsBid.setUpdateTime(new Date());
        dneedsBid.setUserId(ucenterUser.getId());
        int i=apiService.installDneedsBid(dneedsBid);

        if(i!=0){
            return new WxplatformResult(WxplatformResultConstant.SUCCESS, "投标成功");
        }else{
            return new WxplatformResult(WxplatformResultConstant.FAILED, "请勿重复投标");

        }    }
    @RequestMapping(value = "/jiedanData",method = RequestMethod.POST)
    @ResponseBody
    private Object jiedan(Model model,HttpServletRequest request){
        byte[]user =  UcenterUserUtils.getUser(request.getSession());

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ConfigRegion configRegion = new ConfigRegion();

        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        String dqaddress = RedisUtil.get("dangqianweizhi"+ucenterUser.getId());
        net.sf.json.JSONObject jsonobject = net.sf.json.JSONObject.fromObject(dqaddress);
        UcenterUserCity usercity= (UcenterUserCity) net.sf.json.JSONObject.toBean(jsonobject,UcenterUserCity.class);
        double lat=usercity.getLat();
        double lng=usercity.getLng();
        DecimalFormat df = new DecimalFormat("######0.00");

        if (null == ucenterUser) {
            model.addAttribute("data", "您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return thymeleaf("/404");
        }
        byte[] address = UcenterUserUtils.getAddress(ucenterUser.getId());
        configRegion = (ConfigRegion) ByteUtils.ByteToObject(address);

        String json = RedisUtil.get(ucenterUser.getId() + "DneedsDemandLNG");
        List<DneedsDemand> removeList = new ArrayList<>();

        DneedsDemandExample dneedsDemandExample = new DneedsDemandExample();
        dneedsDemandExample.createCriteria().andCityEqualTo(configRegion.getRegionId()).andDemandTypeLessThan(1);
        List<DneedsDemand> list=apiService.selectDneedsDemand(dneedsDemandExample);

        String json1 = JSONArray.toJSONString(list);
        RedisUtil.del_add(ucenterUser.getId() + "DneedsDemandLNG", json1);
        DneedsBidExample example = new DneedsBidExample();
        example.createCriteria().andUserIdEqualTo(ucenterUser.getId());
        List<DneedsBid>bids=apiService.selectDneedsBid(example);
        for(DneedsDemand dneedsDemand :list){
            for(DneedsBid dneedsBid :bids){
                if(dneedsDemand.getId()==dneedsBid.getDemandId()){
                    removeList.add(dneedsDemand);
                }
            }
            com.github.wenhao.geohash.GeoHash geoHash = com.github.wenhao.geohash.GeoHash.fromLong(dneedsDemand.getGeoCode());
            double distance = geoHash.distance(lat, lng);

            double distanceKm = distance / 1000;
            dneedsDemand.setDistanceKm(Double.valueOf(df.format(distanceKm)));
        }
        list.removeAll(removeList);
        Map map = new HashMap();
        map.put("lng", lng);
        map.put("lat", lat);
        map.put("list", list);
        return new WxplatformResult(WxplatformResultConstant.SUCCESS, map);
    }
    @RequestMapping(value="/toubiaoData",method = RequestMethod.POST)
    @ResponseBody
    private Object toubiaoData(Model model,HttpServletRequest request){
        byte[]user =  UcenterUserUtils.getUser(request.getSession());
        DecimalFormat df = new DecimalFormat("######0.00");

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ConfigRegion configRegion = new ConfigRegion();

        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        String dqaddress = RedisUtil.get("dangqianweizhi"+ucenterUser.getId());
        net.sf.json.JSONObject jsonobject = net.sf.json.JSONObject.fromObject(dqaddress);
        UcenterUserCity usercity= (UcenterUserCity) net.sf.json.JSONObject.toBean(jsonobject,UcenterUserCity.class);
        double lat=usercity.getLat();
        double lng=usercity.getLng();
        DneedsBidExample example = new DneedsBidExample();
        example.createCriteria().andUserIdEqualTo(ucenterUser.getId());
        List<DneedsBid>bids=apiService.selectDneedsBid(example);
        List<DneedsDemand> list = new ArrayList<>();
        List<DneedsDemand> weifukuanlist = new ArrayList<>();

        List<DneedsDemand> weixuanlist = new ArrayList<>();

        for(DneedsBid dneedsBid:bids){
            if(dneedsBid.getState()==0){
                DneedsDemandExample dneedsDemandExample = new DneedsDemandExample();
                dneedsDemandExample.createCriteria().andIdEqualTo(dneedsBid.getDemandId());
                DneedsDemand dneedsDemand = apiService.getDneedsDemand(dneedsDemandExample);
                com.github.wenhao.geohash.GeoHash geoHash = com.github.wenhao.geohash.GeoHash.fromLong(dneedsDemand.getGeoCode());
                double distance = geoHash.distance(lat, lng);
                double distanceKm = distance / 1000;
                dneedsDemand.setDistanceKm(Double.valueOf(df.format(distanceKm)));
                list.add(dneedsDemand);
            }else if(dneedsBid.getState()==1){
                DneedsDemandExample dneedsDemandExample = new DneedsDemandExample();
                dneedsDemandExample.createCriteria().andIdEqualTo(dneedsBid.getDemandId());
                DneedsDemand dneedsDemand = apiService.getDneedsDemand(dneedsDemandExample);
                if(dneedsDemand.getDemandType()==2){
                    com.github.wenhao.geohash.GeoHash geoHash = com.github.wenhao.geohash.GeoHash.fromLong(dneedsDemand.getGeoCode());
                    double distance = geoHash.distance(lat, lng);
                    double distanceKm = distance / 1000;
                    dneedsDemand.setDistanceKm(Double.valueOf(df.format(distanceKm)));
                    weifukuanlist.add(dneedsDemand);
                }else{

                }
            }
            else{
                DneedsDemandExample dneedsDemandExample = new DneedsDemandExample();
                dneedsDemandExample.createCriteria().andIdEqualTo(dneedsBid.getDemandId());
                DneedsDemand dneedsDemand = apiService.getDneedsDemand(dneedsDemandExample);
                com.github.wenhao.geohash.GeoHash geoHash = com.github.wenhao.geohash.GeoHash.fromLong(dneedsDemand.getGeoCode());
                double distance = geoHash.distance(lat, lng);
                double distanceKm = distance / 1000;
                dneedsDemand.setDistanceKm(Double.valueOf(df.format(distanceKm)));
                weixuanlist.add(dneedsDemand);
            }

        }
        Map<String,List<DneedsDemand>> map = new HashMap<>();
        map.put("yitou",list);
        map.put("weixuan",weixuanlist);
        map.put("weifukuanlist",weifukuanlist);

        return new WxplatformResult(WxplatformResultConstant.SUCCESS, map);

    }
    @RequestMapping(value="/zhizuobiaoData",method = RequestMethod.POST)
    @ResponseBody
    private Object zhizuobiaoData(Model model,HttpServletRequest request){
        Map<String,List<DneedsDemand>> map = new HashMap<>();

        byte[]user =  UcenterUserUtils.getUser(request.getSession());
        DecimalFormat df = new DecimalFormat("######0.00");

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ConfigRegion configRegion = new ConfigRegion();

        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        String dqaddress = RedisUtil.get("dangqianweizhi"+ucenterUser.getId());
        net.sf.json.JSONObject jsonobject = net.sf.json.JSONObject.fromObject(dqaddress);
        UcenterUserCity usercity= (UcenterUserCity) net.sf.json.JSONObject.toBean(jsonobject,UcenterUserCity.class);
        double lat=usercity.getLat();
        double lng=usercity.getLng();
        DneedsBidExample example = new DneedsBidExample();
        example.createCriteria().andUserIdEqualTo(ucenterUser.getId()).andStateEqualTo(1);
        List<DneedsBid>bids=apiService.selectDneedsBid(example);
        List<DneedsDemand> list = new ArrayList<>();
        List<DneedsDemand> ysclist = new ArrayList<>();
        List<DneedsDemand> jujueclist = new ArrayList<>();
        List<DneedsDemand> ywcclist = new ArrayList<>();

        for(DneedsBid dneedsBid:bids){
            DneedsDemandExample dneedsDemandExample = new DneedsDemandExample();
            dneedsDemandExample.createCriteria().andIdEqualTo(dneedsBid.getDemandId()).andDemandTypeBetween(3,7);
            DneedsDemand dneedsDemand = apiService.getDneedsDemand(dneedsDemandExample);
            if(null!=dneedsDemand){
                if(dneedsDemand.getDemandType()==3){
                    com.github.wenhao.geohash.GeoHash geoHash = com.github.wenhao.geohash.GeoHash.fromLong(dneedsDemand.getGeoCode());
                    double distance = geoHash.distance(lat, lng);
                    double distanceKm = distance / 1000;
                    dneedsDemand.setDistanceKm(Double.valueOf(df.format(distanceKm)));
                    list.add(dneedsDemand);
                }else if(dneedsDemand.getDemandType()==4){
                    com.github.wenhao.geohash.GeoHash geoHash = com.github.wenhao.geohash.GeoHash.fromLong(dneedsDemand.getGeoCode());
                    double distance = geoHash.distance(lat, lng);
                    double distanceKm = distance / 1000;
                    dneedsDemand.setDistanceKm(Double.valueOf(df.format(distanceKm)));
                    ysclist.add(dneedsDemand);
                }else if(dneedsDemand.getDemandType()==5){
                    com.github.wenhao.geohash.GeoHash geoHash = com.github.wenhao.geohash.GeoHash.fromLong(dneedsDemand.getGeoCode());
                    double distance = geoHash.distance(lat, lng);
                    double distanceKm = distance / 1000;
                    dneedsDemand.setDistanceKm(Double.valueOf(df.format(distanceKm)));
                    jujueclist.add(dneedsDemand);
                }else if(dneedsDemand.getDemandType()==6){
                    com.github.wenhao.geohash.GeoHash geoHash = com.github.wenhao.geohash.GeoHash.fromLong(dneedsDemand.getGeoCode());
                    double distance = geoHash.distance(lat, lng);
                    double distanceKm = distance / 1000;
                    dneedsDemand.setDistanceKm(Double.valueOf(df.format(distanceKm)));
                    jujueclist.add(dneedsDemand);
                }else if(dneedsDemand.getDemandType()==7){
                    com.github.wenhao.geohash.GeoHash geoHash = com.github.wenhao.geohash.GeoHash.fromLong(dneedsDemand.getGeoCode());
                    double distance = geoHash.distance(lat, lng);
                    double distanceKm = distance / 1000;
                    dneedsDemand.setDistanceKm(Double.valueOf(df.format(distanceKm)));
                    ywcclist.add(dneedsDemand);
                }

            }

        }
        map.put("list",list);
        map.put("ysclist",ysclist);
        map.put("jujueclist",jujueclist);
        map.put("ywcclist",ywcclist);
        return new WxplatformResult(WxplatformResultConstant.SUCCESS, map);

    }
    @RequestMapping(value="/scsjt")
    public String scsjt(HttpServletRequest request,Model model,int id){
        byte[]user =  UcenterUserUtils.getUser(request.getSession());

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        DneedsBidExample example = new DneedsBidExample();
        example.createCriteria().andDemandIdEqualTo(id).andUserIdEqualTo(ucenterUser.getId()).andStateEqualTo(1);
        DneedsBid dneedsBid = apiService.getDneedsBid(example);
        if(null!=dneedsBid){
            model.addAttribute("demandId",id);
            return thymeleaf("/SJS/SJS_Scsjt");

        }
        model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
        return  thymeleaf("/404");
    }
    @RequestMapping(value="/qdtu")
    public String qdtu(HttpServletRequest request,Model model,int id){
        byte[]user =  UcenterUserUtils.getUser(request.getSession());

        if(null==user){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        DneedsBidExample example = new DneedsBidExample();
        example.createCriteria().andDemandIdEqualTo(id).andUserIdEqualTo(ucenterUser.getId()).andStateEqualTo(1);
        DneedsBid dneedsBid = apiService.getDneedsBid(example);
        if(null!=dneedsBid){
            model.addAttribute("demandId",id);
            return thymeleaf("/SJS/SJS_Scwlt");

        }
        model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
        return  thymeleaf("/404");
    }
}
